package com.playcrab.adouban;

import java.util.List;

import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.google.gdata.data.douban.ReviewEntry;
//import com.google.gdata.data.extensions.Rating;

public class ReviewListAdapter extends ArrayAdapter {

		private static final String LOG_TAG = "BookListAdapter";
		Activity context;
		LayoutInflater inflater;
		WebView userIcon;
		TextView username, replyTo, updateBody, via, timestamp;
		List<ReviewEntry> reviews;

		public ReviewListAdapter(Activity context, List<ReviewEntry> reviews) {
			super(context, R.layout.review_row, reviews);
			inflater = LayoutInflater.from(context);

			this.context = context;
			this.reviews = reviews;
			// TODO Auto-generated constructor stub
		}
		
		public View getView(int position, View convertView, ViewGroup parent) {

			View row = convertView;
			row = inflater.inflate(R.layout.review_row, null);
			row.setLongClickable(true);
			row.setClickable(true);
			
			ReviewEntry reviewEntry = reviews.get(position);

			row.setTag(reviewEntry);
			if (reviewEntry == null) {
				return row;
			}
			
			if (!reviewEntry.getTitle().isEmpty()) {
				TextView review_title = (TextView) row.findViewById(R.id.review_title);
				review_title.setText(reviewEntry.getTitle().getPlainText());
			}
			
			if (!reviewEntry.getAuthors().isEmpty()) {
				TextView review_user = (TextView) row.findViewById(R.id.review_user);
				review_user.setText("by  " + reviewEntry.getAuthors().get(0).getName());
//				System.out.println("author URI is : "
//						+ reviewEntry.getAuthors().get(0).getUri());
			}
//			System.out.println("updated date is : " + reviewEntry.getUpdated());
//			System.out.println("published date is : " + reviewEntry.getPublished());
			if (reviewEntry.getSummary() != null) {
				TextView review_summary = (TextView) row.findViewById(R.id.review_summary);
				review_summary.setText(reviewEntry.getSummary().getPlainText());
			}
//			Rating rating = reviewEntry.getRating();
//			if (rating != null)
//				System.out.println("Rating : max is " + rating.getMax()
//						+ " min is " + rating.getMin() + " numRaters is "
//						+ rating.getNumRaters() + " average is "
//						+ rating.getAverage() + " value is " + rating.getValue());
			
			return row;
		}

	    @Override
	    public boolean areAllItemsEnabled() {
	        return true;
	    }

	    @Override
	    public boolean isEnabled(int position) {
	        return true;
	    }
	    
}
